/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package panelAdmin;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Chinh
 */
public class ListCustommer {

    public ListCustommer() {
    }

    public ArrayList<Customer> LoadListCustomer() {
        ArrayList<Customer> listCustommer = new ArrayList<Customer>();
        File f = new File("Customer.txt");
        try {
            FileReader fr = new FileReader(f);
            LineNumberReader lnr = new LineNumberReader(fr);
            String customer = "";
            while (true) {
                customer = lnr.readLine();
                if (customer == null) {
                    break;
                }
                if (customer.contains("#")) {
                    continue;
                }
                String id = "";
                String password = "";
                String name = "";
                String phoneNumber = "";
                String ba;
                long balance = 0;
                StringTokenizer tokens = new StringTokenizer(customer, "|");
                for (int i = 0; i < 5; i++) {
                    if (i == 0) {
                        id = tokens.nextToken();
                    }
                    if (i == 1) {
                        password = tokens.nextToken();
                    }
                    if (i == 2) {
                        name = tokens.nextToken();
                    }
                    if (i == 3) {
                        phoneNumber = tokens.nextToken();
                    }
                    if (i == 4) {
                        ba = tokens.nextToken();
                        balance = Integer.parseInt(ba);
                    }
                }
                listCustommer.add(new Customer(id, password, name, phoneNumber, balance));
            }
            lnr.close();
            fr.close();
        } catch (FileNotFoundException ex) {
        } catch (IOException ex) {
        }
        return listCustommer;
    }

    public void SaveListCustomer(ArrayList<Customer> listCustomer) {
        File f = new File("Customer.txt");
        try {
            FileWriter fw = new FileWriter(f);
            PrintWriter pw = new PrintWriter(fw);
            pw.println("#ID|Passwrord|Name|PhoneNumber|Balance");
            for (int i = 0; i < listCustomer.size(); i++) {
                Customer cus = listCustomer.get(i);
                pw.println(cus.getId() + "|" + cus.getPassword() + "|" + cus.getName() + "|" + cus.getPhoneNumber() + "|" + cus.getBalance());
            }
            pw.close();
            fw.close();
        } catch (IOException ex) {
            Logger.getLogger(ListCustommer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

//class App2 {
//
//    public static void main(String[] args) {
//        ListCustommer list = new ListCustommer();
//        ArrayList<Customer> listCus = list.LoadListCustomer();
//        System.out.println(listCus.get(0));
//        listCus.get(0).setName("Nguyễn Duy Đăng");
//        list.SaveListCustomer(listCus);
//    }
//}
